package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.xiangxian.Brand;
import com.tongzhan.entity.xiangxian.Fengcai;
import com.tongzhan.utils.SnowFlakeUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**品牌模块
 * @author hongjianbin
 * @date 2020/7/20
 */
@RequestMapping("/barnd")
@RestController
public class BrandController extends BaseController {
    @PostMapping("/addbarnd")
    public Return addbarnd(Brand brand) {
        if(brand==null){
            return  Return.getErrorResult("参数为空");
        }
        if (brand.getContent().trim().length() > 200) {
            return Return.getErrorResult("品牌内容过长");
        }
        if (brand.getInfo().trim().length() > 200) {
            return Return.getErrorResult("简介过长");
        }
        if (brand.getName().length() > 20) {
            return Return.getErrorResult("品牌名称过长");
        }
        if(brand.getName()!=null) {
            long l = fengcaiService.findBrandByName(brand.getName());
            if(l>0){
                return  Return.getErrorResult("品牌名称已存在");
            }
        }
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(3, 3);
        brand.setBid(String.valueOf(snowFlake.nextId()));

        long l = fengcaiService.addbarnd(brand);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 品牌详情
     * @param bid
     * @return
     */
    @PostMapping("/brandDetail")
    public Return brandDetail(String bid ){
        if(bid==null){
            return  Return.getErrorResult("缺少id参数!");
        }
        Brand brand = fengcaiService.brandDetail(bid);
        return Return.getSuccessResult(brand);
    }

    /**
     *
     * @param name 品牌名称
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/brandList")
    public Return brandList(String name,Integer page,Integer size ){
        page = page == null ? 0 : page - 1;
        Page<Brand> list = fengcaiService.brandList(name, page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list",list.getContent());
        map.put("total",list.getTotalElements());
        map.put("totalpage",list.getTotalPages());
        return Return.getSuccessResult(map);
    }
}
